﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;

namespace PS.WebParts
{       
    public class TemplateBuilder
    {
        public TemplateBuilder()
        {

        }

        static void Main(string[] args)
        {
            try
            {
                string url = args[0];

                DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory);

                dir = dir.Parent;

                string destination = dir.FullName;

                if (!File.Exists(url))
                    Console.WriteLine("File doesn't exist: " + url);
                else
                {
                    Assembly assm = Assembly.LoadFrom(url);
                    
                    Type t1 = assm.GetType("PS.WebParts.Rollup.ResourceTemplates.TemplateManager");

                    object oo = Activator.CreateInstance(t1);

                    t1.InvokeMember("CreateFiles", BindingFlags.InvokeMethod,
                                        Type.DefaultBinder, oo, new object[] { destination });
                }
            }
            catch (Exception ex)
            {
                Exception innerEx = ex;

                while (innerEx != null)
                {
                    Console.WriteLine(innerEx.Message);
                    Console.WriteLine(innerEx.StackTrace);

                    innerEx = innerEx.InnerException;
                }
            }
        }        
    }
}